package com.alibaba.otter.node.etl.autoconfigure;

import com.alibaba.otter.common.push.datasource.DataSourceHandler;
import com.alibaba.otter.common.push.datasource.media.MediaPushDataSourceHandler;
import com.alibaba.otter.node.etl.common.datasource.impl.DBDataSourceService;
import com.alibaba.otter.node.etl.common.db.dialect.DbDialectFactory;
import com.alibaba.otter.node.etl.common.db.dialect.DbDialectGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.support.lob.DefaultLobHandler;

import java.util.ArrayList;
import java.util.List;

@Configuration(proxyBeanMethods = false)
public class NodeDataBaseAutoConfiguration {


    @Bean
    public DefaultLobHandler defaultLobHandler(){
        DefaultLobHandler defaultLobHandler = new DefaultLobHandler();
        defaultLobHandler.setStreamAsLob(true);
        return defaultLobHandler;
    }


    @Bean
    public DbDialectGenerator dbDialectGenerator(DefaultLobHandler defaultLobHandler){
        DbDialectGenerator dbDialectGenerator = new DbDialectGenerator();
        dbDialectGenerator.setDefaultLobHandler(defaultLobHandler);
        dbDialectGenerator.setOracleLobHandler(defaultLobHandler);
        return dbDialectGenerator;
    }

    @Bean
    public MediaPushDataSourceHandler mediaPushDataSourceHandler(){
        return new MediaPushDataSourceHandler();
    }

    @Bean
    public DBDataSourceService dataSourceService(MediaPushDataSourceHandler mediaPushDataSourceHandler){
        DBDataSourceService dbDataSourceService = new DBDataSourceService();
        List<DataSourceHandler> sourceHandlers = new ArrayList<>();
        sourceHandlers.add(mediaPushDataSourceHandler);
        dbDataSourceService.setDataSourceHandlers(sourceHandlers);
        return dbDataSourceService;
    }

    @Bean
    public DbDialectFactory dbDialectFactory(DBDataSourceService dataSourceService,DbDialectGenerator dbDialectGenerator){
        DbDialectFactory dbDialectFactory = new DbDialectFactory();
        dbDialectFactory.setDataSourceService(dataSourceService);
        dbDialectFactory.setDbDialectGenerator(dbDialectGenerator);
        return dbDialectFactory;
    }



}
